home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / xgui / sources / ui_colEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  13.4 KB  |  442 lines

  1. /*****************************************************************************
  2.   FILE           : ui_colEdit.c
  3.   SHORTNAME      : colEdit.c
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : functions to manipulate the text, background and 
  7.            selection color
  8.   NOTES          : uses the fixed palette defined in ui_color.c
  9.  
  10.   AUTHOR         : Ralf Huebner
  11.   DATE           : 27.5.1992
  12.  
  13.   CHANGED BY     :
  14.   IDENTIFICATION : @(#)ui_colEdit.c    1.12 3/2/94
  15.   SCCS VERSION   : 1.12
  16.   LAST CHANGE    : 3/2/94
  17.  
  18.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  19.              
  20. ******************************************************************************/
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #include <X11/Xlib.h>
  27. #include <X11/Intrinsic.h>
  28. #include <X11/StringDefs.h>
  29. #include <X11/Shell.h>
  30. #include <X11/Xaw/Simple.h>
  31. #include <X11/Xaw/Box.h>
  32. #include <X11/Xaw/Form.h>
  33. #include <X11/Xaw/Toggle.h>
  34.  
  35. #include "ui.h"
  36. #include "ui_xWidgets.h"
  37. #include "ui_color.h"
  38. #include "ui_mainP.h"
  39. #include "ui_main.h"
  40.  
  41. #include "ui_colEdit.ph"
  42.  
  43.  
  44. /*****************************************************************************
  45.   FUNCTION : ui_cancelColorEditPannel
  46.  
  47.   PURPOSE  : callback if the cancel button is pressed
  48.   RETURNS  : void
  49.   NOTES    : closes the pannel
  50.  
  51.   UPDATE   :
  52. ******************************************************************************/
  53.  
  54. static void ui_cancelColorEditPannel (Widget w,Widget pannel,caddr_t call_data)
  55.  
  56. {
  57.     XtDestroyWidget (pannel);
  58. }
  59.  
  60.  
  61. /*****************************************************************************
  62.   FUNCTION : ui_closeColorEditPannel
  63.  
  64.   PURPOSE  : sets the selected colors in the current display
  65.   RETURNS  : void
  66.   NOTES    :
  67.  
  68.   UPDATE   :
  69. ******************************************************************************/
  70.  
  71. static void ui_closeColorEditPannel (Widget w, Widget pannel, caddr_t call_data)
  72.  
  73. {
  74.     (ui_set_displayPtr->setup).backgroundColorIndex = ui_currentBackCol;
  75.     (ui_set_displayPtr->setup).selectionColorIndex = ui_currentSelCol;
  76.     (ui_set_displayPtr->setup).textColorIndex = ui_currentTextCol;
  77.     XtDestroyWidget (pannel);
  78. }
  79.  
  80.  
  81. /*****************************************************************************
  82.   FUNCTION : ui_getDisplayColors
  83.  
  84.   PURPOSE  : reads the color indices out of the display
  85.   RETURNS  : void
  86.   NOTES    :
  87.  
  88.   UPDATE   :
  89. ******************************************************************************/
  90.  
  91. static void ui_getDisplayColors (void)
  92.  
  93. {
  94.     ui_currentBackCol = (ui_set_displayPtr->setup).backgroundColorIndex;
  95.     ui_currentSelCol = (ui_set_displayPtr->setup).selectionColorIndex;
  96.     ui_currentTextCol = (ui_set_displayPtr->setup).textColorIndex;
  97. }
  98.  
  99.  
  100. /*****************************************************************************
  101.   FUNCTION : ui_xCreateColButtonItem
  102.  
  103.   PURPOSE  : creates a widget for a color button
  104.   RETURNS  : Widget
  105.   NOTES    : the color is defined in pixel
  106.  
  107.   UPDATE   :
  108. ******************************************************************************/
  109.  
  110. static Widget ui_xCreateColButtonItem (Widget parent, unsigned long pixel, 
  111.     int xsize, int ysize, Widget left, Widget top)
  112.  
  113. {
  114.     Cardinal n;
  115.     Widget   w;
  116.     Arg         args[15];
  117.  
  118.     n = 0;
  119.     XtSetArg(args[n], XtNinternalHeight, 1); n++;
  120.     XtSetArg(args[n], XtNinternalWidth , 1); n++;
  121.     XtSetArg(args[n], XtNfromVert , top);  n++;
  122.     XtSetArg(args[n], XtNfromHoriz, left);  n++;
  123.     XtSetArg(args[n], XtNleft  , XtChainLeft); n++;
  124.     XtSetArg(args[n], XtNright , XtChainLeft); n++;
  125.     XtSetArg(args[n], XtNtop   , XtChainTop); n++;
  126.     XtSetArg(args[n], XtNbottom, XtChainTop); n++;
  127.     XtSetArg(args[n], XtNwidth, xsize); n++;
  128.     XtSetArg(args[n], XtNheight, ysize); n++;
  129.     XtSetArg(args[n], XtNbackground, pixel); n++;
  130.     w = XtCreateManagedWidget("", commandWidgetClass, parent, args, n);
  131.     return(w);
  132. }
  133.  
  134.  
  135. /*****************************************************************************
  136.   FUNCTION : ui_createColWidgets
  137.  
  138.   PURPOSE  : creates the widgets for all colors
  139.   RETURNS  : void
  140.   NOTES    : 
  141.  
  142.   UPDATE   :
  143. ******************************************************************************/
  144.  
  145. static void ui_createColWidgets (Widget parent)
  146.  
  147. {
  148.     Widget top, left;
  149.     unsigned long pixel;
  150.     int i, ix, iy;
  151.  
  152.     for (i=0; i<UI_MAX_EDIT_COLS; i++) {
  153.         ix = i MOD UI_VERT_COLS;
  154.         iy = i DIV UI_VERT_COLS;
  155.         if (ix-1 < 0)
  156.            left = NULL;
  157.         else
  158.            left = ui_colWidget[iy * UI_VERT_COLS + ix - 1];
  159.            
  160.         if (iy-1 < 0)
  161.            top = NULL;
  162.         else
  163.            top = ui_colWidget[iy * UI_VERT_COLS - 1];
  164.         pixel = ui_editColor[i];
  165.         ui_colWidget[i] = ui_xCreateColButtonItem (parent, pixel,
  166.                          colWidgetXsize, colWidgetYsize, left, top);
  167.     }
  168. }
  169.  
  170.  
  171. /*****************************************************************************
  172.   FUNCTION : ui_createTestPannel
  173.  
  174.   PURPOSE  : creates the test pannel for the selected colors
  175.   RETURNS  :
  176.   NOTES    : the sizes are absolute and depend on the
  177.              vars in ui_redrawColorWindow
  178.  
  179.   UPDATE   :
  180. ******************************************************************************/
  181.  
  182. static Widget ui_createTestPannel (Widget parent, Widget left, Widget top)
  183.  
  184. {
  185.     Cardinal n;
  186.     Widget   w;
  187.     Arg         args[15];
  188.  
  189.     n = 0;
  190.     XtSetArg(args[n], XtNheight, 72); n++; /* 58 */
  191.     XtSetArg(args[n], XtNwidth, 234); n++;
  192.     XtSetArg(args[n], XtNfromVert , top);  n++;
  193.     XtSetArg(args[n], XtNfromHoriz, left);  n++;
  194.     XtSetArg(args[n], XtNleft  , XtChainLeft); n++;
  195.     XtSetArg(args[n], XtNright , XtChainLeft); n++;
  196.     XtSetArg(args[n], XtNtop   , XtChainTop); n++;
  197.     XtSetArg(args[n], XtNbottom, XtChainTop); n++;
  198.     w = XtCreateManagedWidget("testPanel", boxWidgetClass, 
  199.                                         parent, args, n);
  200.     return(w);
  201.  
  202. }
  203.  
  204.  
  205. /*****************************************************************************
  206.   FUNCTION : ui_redrawColorWindow
  207.  
  208.   PURPOSE  : draws the test pannel
  209.   RETURNS  :
  210.   NOTES    : in the first line is the colorscale from red to green
  211.              in the second are units in the selection color
  212.              the background and text color are also shown
  213.  
  214.   UPDATE   :
  215. ******************************************************************************/
  216.  
  217. static void ui_redrawColorWindow (void)
  218.  
  219. {
  220.     static int xsize = 16;
  221.     static int ysize = 16;
  222.     static int xspace = 32;
  223.     static int yspace = 32;
  224.     static int xoffset = 14;
  225.     static int yoffset = 14;
  226.  
  227.     char buf[10];
  228.     int i;
  229.  
  230.     XSetWindowBackground(ui_display, colWindow,
  231.                ui_editColor[ui_currentBackCol]);
  232.     XClearWindow (ui_display, colWindow);
  233.     for (i=0; i<7; i++) {
  234.         XSetForeground(ui_display, colGC,
  235.                        ui_col_rangePixels[i*5]);
  236.         XFillRectangle(ui_display, colWindow, colGC,
  237.                        xoffset + i*xspace, yoffset, (unsigned int) xsize,                              (unsigned int) ysize);
  238.         XSetForeground(ui_display, colGC, ui_editColor[ui_currentTextCol]);
  239.         sprintf (buf, "u%d", i); 
  240.         XDrawString(ui_display, colWindow, colGC,
  241.                xoffset + i*xspace, yoffset-2, buf, (int) strlen(buf));
  242.     }
  243.     for (i=0; i<7; i++) {
  244.         XSetForeground(ui_display, colGC, ui_editColor[ui_currentSelCol]);
  245.         XFillRectangle(ui_display, colWindow, colGC,
  246.                        xoffset + i*xspace, yoffset+yspace, (unsigned int) xsize,                       (unsigned int) ysize);
  247.         XSetForeground(ui_display, colGC, ui_editColor[ui_currentTextCol]);
  248.         sprintf (buf, "u%d", i+7); 
  249.         XDrawString(ui_display, colWindow, colGC,
  250.                xoffset + i*xspace, yoffset+yspace-2, buf, (int) strlen(buf));
  251.     }
  252.  
  253. }
  254.  
  255.  
  256. /*****************************************************************************
  257.   FUNCTION : ui_colorUpdateProc
  258.  
  259.   PURPOSE  : selects which color to edit
  260.   RETURNS  : void
  261.   NOTES    :
  262.  
  263.   UPDATE   :
  264. ******************************************************************************/
  265.  
  266. static void ui_colorUpdateProc (Widget w, int color, caddr_t call_data)
  267.  
  268. {
  269.     switch (currentSelection) {
  270.         case UI_SELECT_BACK_COLOR: ui_currentBackCol = color;
  271.                                    break;
  272.         case UI_SELECT_SEL_COLOR : ui_currentSelCol = color;
  273.                                    break;
  274.         case UI_SELECT_TEXT_COLOR: ui_currentTextCol = color;
  275.                                    break;
  276.     }
  277.     ui_redrawColorWindow();
  278. }
  279.  
  280.  
  281. /*****************************************************************************
  282.   FUNCTION : ui_timerProc
  283.  
  284.   PURPOSE  : redraws the test window
  285.   RETURNS  : void
  286.   NOTES    : callback from a timer. called 1 sec after the edit pannel is
  287.              popped up. this is nessecary, because a redraw function can't
  288.              called directly
  289.  
  290.   UPDATE   :
  291. ******************************************************************************/
  292.  
  293. static void ui_timerProc (caddr_t client_data, XtIntervalId *timerId)
  294.  
  295. {
  296.     ui_redrawColorWindow();
  297. }
  298.  
  299.  
  300. /*****************************************************************************
  301.   FUNCTION : ui_colorWindowEventProc
  302.  
  303.   PURPOSE  : redraws the test window
  304.   RETURNS  : void
  305.   NOTES    : called by the event loop if redraw is nessecary
  306.  
  307.   UPDATE   :
  308. ******************************************************************************/
  309.  
  310. static void ui_colorWindowEventProc (Widget w, Display *display, XEvent *event)
  311.  
  312. {
  313.     if ((event->type == Expose) AND (event->xexpose.count == 0))
  314.         ui_redrawColorWindow();        
  315. }
  316.  
  317.  
  318. /*****************************************************************************
  319.   FUNCTION : ui_setToggles
  320.  
  321.   PURPOSE  : swiches the radio buttons
  322.   RETURNS  : void
  323.   NOTES    :
  324.  
  325.   UPDATE   :
  326. ******************************************************************************/
  327.  
  328. static void ui_setToggles (int index, int *new, int max, Widget widgets[])
  329.  
  330. {
  331.    int i;
  332.  
  333.    for(i=0; i<max; i++)
  334.        ui_xSetToggleState(widgets[i], FALSE);
  335.    ui_xSetToggleState(widgets[index], TRUE);
  336.    *new = index;
  337. }
  338.  
  339.  
  340. /*****************************************************************************
  341.   FUNCTION : ui_selectColorItemProc
  342.  
  343.   PURPOSE  : toggles the specified button
  344.   RETURNS  : void
  345.   NOTES    :
  346. ******************************************************************************/
  347.  
  348. static void ui_selectColorItemProc (Widget w, int value, caddr_t call_data)
  349.  
  350. {
  351.     ui_setToggles(value, ¤tSelection, noOfColToggles, colorToggle);
  352. }
  353.  
  354.  
  355. /*****************************************************************************
  356.   FUNCTION : ui_createColorEditPannel
  357.  
  358.   PURPOSE  : creates the color edit pannel
  359.   RETURNS  : void
  360.   NOTES    :
  361.  
  362.   UPDATE   :
  363. ******************************************************************************/
  364.  
  365. void ui_createColorEditPannel (Widget w, Widget button, caddr_t call_data)
  366.  
  367. {
  368.     int n, i;
  369.     Arg arg[10];
  370.     Position xPos, yPos;
  371.     Dimension width, height;
  372.     Widget colEditPannel, pannel, border, done;
  373.     Widget cancel, colframe, testPannel;
  374.  
  375.     n = 0;
  376.     XtSetArg (arg[0], XtNwidth, &width); n++;
  377.     XtSetArg (arg[1], XtNheight, &height); n++;
  378.     XtGetValues (button, arg, (unsigned int) n);
  379.  
  380.     XtTranslateCoords (button, (Position) (width / 2), (Position) (height / 2), &xPos, &yPos);
  381.  
  382.     n = 0;
  383.     XtSetArg(arg[n], XtNx, xPos); n++;
  384.     XtSetArg(arg[n], XtNy, yPos); n++;
  385.  
  386.     colEditPannel = XtCreatePopupShell ("color edit", transientShellWidgetClass, button, arg, (unsigned int) n);
  387.  
  388.     border = XtCreateManagedWidget("border", boxWidgetClass,
  389.                                    colEditPannel, NULL, ZERO);
  390.     pannel = XtCreateManagedWidget("pannel", formWidgetClass, 
  391.                                    border, NULL, ZERO);
  392.     colframe = XtCreateManagedWidget("colframe", formWidgetClass, 
  393.                                    pannel, NULL, ZERO);
  394.  
  395.     ui_createColWidgets (colframe);
  396.  
  397.     done = ui_xCreateButtonItem ("done", border, NULL, NULL);
  398.     cancel = ui_xCreateButtonItem ("cancel", border, NULL, done);
  399.  
  400.     colorToggle[0] = ui_xCreateToggleItem ("text", pannel, NULL,
  401.                      NULL, colframe);
  402.     colorToggle[1] = ui_xCreateToggleItem ("background", pannel, NULL,
  403.                      NULL, colorToggle[0]);
  404.     
  405.     colorToggle[2] = ui_xCreateToggleItem ("selection", pannel, NULL,
  406.                      NULL, colorToggle[1]);
  407.      
  408.     ui_getDisplayColors();
  409.  
  410.     testPannel = ui_createTestPannel(pannel, colorToggle[0], colframe);
  411.  
  412.     XtAddCallback (done, XtNcallback, (XtCallbackProc) ui_closeColorEditPannel,
  413.                    (caddr_t) colEditPannel);
  414.     XtAddCallback (cancel, XtNcallback, (XtCallbackProc) ui_cancelColorEditPannel, (caddr_t) colEditPannel);
  415.  
  416.     XtAddCallback (colorToggle[0], XtNcallback, (XtCallbackProc) ui_selectColorItemProc, (caddr_t) UI_SELECT_TEXT_COLOR);
  417.     XtAddCallback (colorToggle[1], XtNcallback, (XtCallbackProc) ui_selectColorItemProc, (caddr_t) UI_SELECT_BACK_COLOR);
  418.     XtAddCallback (colorToggle[2], XtNcallback, (XtCallbackProc) ui_selectColorItemProc, (caddr_t) UI_SELECT_SEL_COLOR);
  419.  
  420.     for (i=0; i<UI_MAX_EDIT_COLS; i++)
  421.         XtAddCallback (ui_colWidget[i], XtNcallback,
  422.                        (XtCallbackProc) ui_colorUpdateProc, (caddr_t) i);
  423.  
  424.     XtAddEventHandler (testPannel, ExposureMask, 
  425.                        FALSE, (XtEventHandler) ui_colorWindowEventProc, ui_display);
  426.  
  427.     ui_checkWindowPosition(colEditPannel);
  428.     XtPopup (colEditPannel, XtGrabNone);
  429.     ui_xDontResizeWidget(colEditPannel); 
  430.  
  431.     ui_setToggles(currentSelection, ¤tSelection,
  432.                   noOfColToggles, colorToggle);
  433.  
  434.     colWindow = XtWindow(testPannel);
  435.  
  436.     colGC = XCreateGC (ui_display, colWindow, 0, 0);
  437.     XSetFont (ui_display, colGC, ui_fontStruct->fid);
  438.     
  439.     XtAppAddTimeOut (ui_appContext, 1000L, (XtTimerCallbackProc) ui_timerProc, NULL);
  440.  
  441. }
  442.